package com.vimi8.ebb.auth.api;

import com.vimi8.ebb.auth.configuration.HttpResponseEntity;
import com.vimi8.ebb.auth.configuration.OAuth2AuthenticationImpl;
import com.vimi8.ebb.auth.dto.OrgDto;
import com.vimi8.ebb.auth.model.Error;
import com.vimi8.ebb.auth.model.Paginator;
import com.vimi8.ebb.auth.service.OrganizationService;
import com.vimi8.ebb.auth.service.RelationService;
import com.vimi8.ebb.auth.utils.PagesUtil;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;

/**
 * Created by wangyang on 2016/12/20.
 */
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.SpringCodegen", date = "2016-09-14T06:39:56.874Z")

@RestController
public class FarmController implements FarmApi {
    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private RelationService relationService;

    @Override
    public HttpResponseEntity<Paginator<List<OrgDto>>> getfarmInfoListByUserId(@ApiParam(value = "页码", required = true) @RequestParam(value = "start", required = true) int start, @ApiParam(value = "每页数据大小", required = true) @RequestParam(value = "size", required = true) int size) {
        String userId= OAuth2AuthenticationImpl.openId();
        int count=organizationService.getOrganizationCountByUser(userId);
        PagesUtil pagesUtil=new PagesUtil();
        pagesUtil.setPageSize(size);
        pagesUtil.setTotalCount(count);
        if(start<1){
            pagesUtil.setCurrentPage(1);
        }

        pagesUtil.setCurrentPage(start);
        try{
            List<OrgDto> farmDtoList=organizationService.getOrganizationInfoByUser(userId,(pagesUtil.getCurrentPage()-1)*size,size);
            Paginator<List<OrgDto>> paginator=new Paginator<List<OrgDto>>(pagesUtil.getCurrentPage(),pagesUtil.getPageSize(),pagesUtil.getTotalPage(),
                    pagesUtil.getTotalCount(),farmDtoList);
            return new HttpResponseEntity<Paginator<List<OrgDto>>>(paginator, HttpStatus.OK);

        }catch (Exception e){
            return new HttpResponseEntity<Paginator<List<OrgDto>>>(new Error().error("请求数据出错："+e.getMessage()),HttpStatus.BAD_REQUEST);
        }
    }



    @Override
    public HttpResponseEntity<String> userFarmRelationPost(@ApiParam(value = "农场id", required = true) @RequestParam(value = "farmId", required = true) String farmId, @ApiParam(value = "关系类型（1：关注，2：加入）", required = true) @RequestParam(value = "relationType", required = true) String relationType) {
        try{
            String id= UUID.randomUUID().toString();
            String  userId=OAuth2AuthenticationImpl.openId();
            int count=relationService.insertRelation(id,userId,farmId,relationType);
            return new HttpResponseEntity<String>(count+"",HttpStatus.OK);
        }catch (Exception e){
            return new HttpResponseEntity<String>("服务器异常："+e.getMessage(),HttpStatus.SERVICE_UNAVAILABLE);
        }
    }

    @Override
    public HttpResponseEntity<String> userFarmRelationDelete(@ApiParam(value = "农场id", required = true) @RequestParam(value = "farmId", required = true) String farmId, @ApiParam(value = "关系类型（1：取消关注，2：取消加入）", required = true) @RequestParam(value = "relationType", required = true) String relationType) {
        try {
            int count = 0;
            String userId = OAuth2AuthenticationImpl.openId();
            //取消关注
            if (relationType.equals("1") && relationService.getUserFarmFans(farmId, userId) > 0) {
                count = relationService.deleteRelation(userId, farmId, relationType);
            }
            return new HttpResponseEntity<String>(count + "", HttpStatus.OK);
        } catch (Exception e) {
            return new HttpResponseEntity<String>("服务器异常：" + e.getMessage(), HttpStatus.SERVICE_UNAVAILABLE);
        }
    }
}
